package com.woniu.service.impl;

import com.cloopen.rest.sdk.CCPRestSmsSDK;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class RonglianSmsService {
    public boolean sendVerificationCode(String phone, String code) {
        try {
            CCPRestSmsSDK sdk = new CCPRestSmsSDK();
            sdk.init("app.cloopen.com", "8883");
            sdk.setAccount("2c94811c9860a9c4019a57d9cbe248ec", "5903823a04a24bba85cfccc592e28607"); // 替换为你的
            sdk.setAppId("2c94811c9860a9c4019a57d9cc9748f3");

//            String templateId = "1"; // ← ✅ 这里必须是你在控制台看到的数字ID
            // 设置短信模板参数：{1}为验证码，{2}为有效分钟数
            String[] params = {code, "5"};

            @SuppressWarnings("unchecked")
            Map<String, Object> result = sdk.sendTemplateSMS(phone, "1", params);

            if (result != null) {
                String statusCode = (String) result.get("statusCode");
                if ("000000".equals(statusCode)) {
                    return true;
                } else {
                    String errorMsg = (String) result.get("statusMsg");
                    System.err.println("短信发送失败，状态码：" + statusCode);
                    System.err.println("错误信息：" + errorMsg);
                    return false;
                }
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}